#!/bin/sh
# PCP QA Test No. 256
#
# Derived metrics and PMNS operations
#
# Copyright (c) 2009 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# For this test, don't load any global derived metric configs by default.
# So the PCP_DERIVED_CONFIG environment variable is set to an empty string.
export PCP_DERIVED_CONFIG=""

status=0	# success is the default!
trap "$sudo rm -f $tmp.*; exit \$status" 0 1 2 3 15

# Derived metric expr dump from 0x8513a48...
# expr node 0x867eb68 type=PLUS left=0x867eb98 right=0x867ed28
_filter()
{
    cat $tmp.out >>$seq_full
    awk <$tmp.out >$tmp.sed '
BEGIN				{ n = 0 }
$1 == "expr" && $2 == "node" && $3 ~ /^0x/	{ print "s/" $3 "/<addr-" n ">/"; n++ }
				{ next }'
    echo "=== sed ===" >>$seq_full
    cat $tmp.sed >>$seq_full
    echo "=== end sed ===" >>$seq_full
    sed -f $tmp.sed <$tmp.out \
    | sed \
	-e '/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/s/[^ ]*.*numpmid/TIMESTAMP ... numpmid/' \
	-e 's/=0x0 /=(nil) /g' \
	-e "s;$tmp;TMP;"
# -e 's/ val=[0-9][0-9]*/ val=<number>/g'
}

# expr node <addr-6> type=NAME left=(nil) right=(nil) save_last=1 [sample.pdu] primary=0
# ...
# [0] inst=-1, val=49794
_filter2()
{
    sed <$tmp.out -e 's/val=/val /' \
    | awk >$tmp.sed2 '
BEGIN			{ n = 0 }
/expr.*sample\./	{ want=1; next }
want == 1 && $3 == "val"	{ if (seen[$4] != "y") {
				    print "s/ val=" $4 "$/ val=<value-" n ">/"
				    n++
				    seen[$4] = "y"
				  }
				}
$1 == "expr"		{ want = 0 }'
    echo "=== sed2 ===" >>$seq_full
    cat $tmp.sed2 >>$seq_full
    echo "=== end sed2 ===" >>$seq_full
    sed -f $tmp.sed2 \
    | sed \
	-e '/pmGetChildren(name="")/s/ [0-9][0-9]* regular/ N regular/' \
	-e '/pmGetChildren/s/returns [0-9][0-9]/returns N/'
}

# real QA test starts here

echo "HOST context ..." | tee -a $seq_full
cat <<End-of-File >$tmp.config
myname.one = sample.long.one
myname.suba.two = 2*sample.long.one
myname.suba.three = 3*sample.long.one
myname.suba.subb.four = 4*sample.long.one
myname.suba.subb.subc.subd.five = 5*sample.long.one
myname.suba.subb.subc.subd.six = 6*sample.long.one
End-of-File
echo | tee -a $seq_full
cat $tmp.config | tee -a $seq_full

for args in myname.suba.subb.subc.subd.six myname.suba.subb myname \
    myname.one.bad myname.suba.subb.subc.bad
do
    echo | tee -a $seq_full
    echo "=== -h local: $args ===" | tee -a $seq_full
    pminfo -h local: -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -h local: -f $args ===" | tee -a $seq_full
    pminfo -h local: -c $tmp.config -f $args >$tmp.out 2>&1
    _filter
done

echo | tee -a $seq_full
echo "ARCHIVE context ..." | tee -a $seq_full
cat <<End-of-File >$tmp.config
sample.one = sample.longlong.one
sample.suba.two = 2*sample.longlong.one
sample.suba.three = 3*sample.longlong.one
sample.suba.subb.four = 4*sample.longlong.one
sample.suba.subb.subc.subd.five = 5*sample.longlong.one
sample.suba.subb.subc.subd.six = 6*sample.longlong.one
End-of-File
echo | tee -a $seq_full
cat $tmp.config | tee -a $seq_full

for args in sample.suba.subb.subc.subd.six sample.suba.subb sample \
    sample.one.bad sample.suba.subb.subc.bad
do
    echo | tee -a $seq_full
    echo "=== -a archives/reduce-1 $args ===" | tee -a $seq_full
    pminfo -a archives/reduce-1 -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -a archives/reduce-1 -f $args ===" | tee -a $seq_full
    pminfo -a archives/reduce-1 -c $tmp.config -f $args >$tmp.out 2>&1
    _filter
done

echo | tee -a $seq_full
echo "LOCAL context ..." | tee -a $seq_full
cat <<End-of-File >$tmp.config
sampledso.one = sampledso.longlong.one
sampledso.suba.two = 2*sampledso.longlong.one
sampledso.suba.three = 3*sampledso.longlong.one
sampledso.suba.subb.four = 4*sampledso.longlong.one
sampledso.suba.subb.subc.subd.five = 5*sampledso.longlong.one
sampledso.suba.subb.subc.subd.six = 6*sampledso.longlong.one
End-of-File
echo | tee -a $seq_full
cat $tmp.config | tee -a $seq_full
export PMDA_LOCAL_SAMPLE=yes

for args in sampledso.suba.subb.subc.subd.six sampledso.suba.subb sampledso \
    sampledso.one.bad sampledso.suba.subb.subc.bad
do
    echo | tee -a $seq_full
    echo "=== -L $args ===" | tee -a $seq_full
    $sudo_local_ctx pminfo -L -c $tmp.config $args >$tmp.out 2>&1
    _filter
    echo "=== -L -f $args ===" | tee -a $seq_full
    $sudo_local_ctx pminfo -L -c $tmp.config -f $args >$tmp.out 2>&1
    _filter \
    | $PCP_AWK_PROG '
NF == 0	{ want = 0; next }
$1 == "sampledso.one"	{ print ""; want = 1 }
$1 ~ /^sampledso\.suba\./	{ print ""; want = 1 }
want == 1		{ print }'
    echo "=== -n .../root -L ===" | tee -a $seq_full
    $sudo_local_ctx pminfo -L -c $tmp.config -n $PCP_VAR_DIR/pmns/root $args >$tmp.out 2>&1
    _filter
    echo "=== -n .../root ===" | tee -a $seq_full
    export PCP_DERIVED_CONFIG=$tmp.config
    src/nameall -n $PCP_VAR_DIR/pmns/root $args >$tmp.out 2>&1
    _filter | sed -e '/Name space load:/s/[0-9.]* msec/N.NN msec/'
    unset PCP_DERIVED_CONFIG
done

echo | tee -a $seq_full
echo "Splice-n-dice into existing PMNS ..." | tee -a $seq_full
cat <<End-of-File >$tmp.config
derived = sample.long.one
derived_tree.ten = sample.long.ten
derived_tree.hundred = sample.long.hundred
derived_tree.sample.long.million = sample.long.million
sample.long.derived = sample.long.one
sample.long.foo.bar.derived = sample.long.one
End-of-File
echo | tee -a $seq_full
cat $tmp.config | tee -a $seq_full

_filter_derived()
{
    # look for metric names containing "derived", apply a blocklist
    grep derived | \
    sed \
	-e '/perfevent.derived/d' \
    #end
}

for args in "" derived derived_tree sample
do
    echo | tee -a $seq_full
    echo "=== $args ===" | tee -a $seq_full
    pminfo -c $tmp.config $args 2>&1 \
    | _filter_derived \
    | sed -e '/bind_expr: error: .*.: Unknown metric name/d' \
    # end
done

echo | tee -a $seq_full
echo "Check diags ..." | tee -a $seq_full
cat <<End-of-File >$tmp.config
derived.tree.one = sample.long.one
derived.tree.ten = sample.long.ten
derived.tree.hundred = sample.long.hundred
derived.tree.a.million = sample.long.million
derived.tree.b.million = sample.long.million
derived.tree.b.cmillion = sample.long.million
sample.long.derived = sample.long.one
sample.long.foo.bar.derived = sample.long.one
End-of-File
cat $tmp.config
export PCP_DERIVED_CONFIG=$tmp.config
#debug# src/torture_pmns -Dall -h local: derived.tree >$tmp.out 2>&1
src/torture_pmns -Dderive,appl1,appl2 -h local: derived.tree >$tmp.out 2>&1
_filter | _filter2

# success, all done
exit
